package com.example.administrator.u17.fragment;

import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.administrator.u17.R;
import com.example.administrator.u17.activity.InputActivity;
import com.example.administrator.u17.activity.SearchClasifyActivity;
import com.example.administrator.u17.activity.SearchTopListDetailActivity;
import com.example.administrator.u17.adapter.SearchAdapter;
import com.example.administrator.u17.config.NetConfig;
import com.example.administrator.u17.modle.MyDbBean;
import com.example.administrator.u17.modle.SearchBean;
import com.squareup.picasso.Picasso;

import org.json.JSONArray;
import org.json.JSONObject;
import org.xutils.DbManager;
import org.xutils.common.Callback;
import org.xutils.ex.DbException;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * Created by Administrator on 2016/11/14.
 */

public class SeachFragment extends Fragment{
    private MyGridView mGridView;
    private SearchAdapter mAdapter;
    private ArrayList<SearchBean.RankingList> mData;
    private ArrayList<SearchBean.TopList> mHeadData;
    private ArrayList<SearchBean.TabList> mHeadDataDetail;
    private String mPath;
    private ConnectivityManager mConnectivityManager;//获取网络通讯类的实例  以便判断是否有网
    private MyDbBean myDb;
    private DbManager mDbManager;
    private ImageView mImageView1,mImageView2,mImageView3,mImageView4;
    private Button mButton;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=View.inflate(getActivity(), R.layout.search_fragment,null);
        //实例化
        mDbManager=x.getDb(new DbManager.DaoConfig());
        mConnectivityManager= (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
        initView(view);
        initData();
        setData();
        setListener();
        return view;
    }

    private void setListener() {
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(getActivity(),SearchClasifyActivity.class);
                String clasifyPath=NetConfig.SEARCH_CLASITY_PATH+mData.get(position).argValue+"&argName="
                        +mData.get(position).argName+"&argCon="/*+2+"&page="*/;

//                String clasifyPath=NetConfig.SEARCH_CLASITY_PATH+mData.get(position).argValue+"&argName="
//                        +mData.get(position).argName+"&argCon=2"+"&page=";
               intent.putExtra("clasifyPath",clasifyPath);
                startActivity(intent);
            }
        });
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(),InputActivity.class);
                startActivity(intent);
            }
        });
    }

    private void initData() {
        mData=new ArrayList<>();
        mHeadData=new ArrayList<>();
        mHeadDataDetail=new ArrayList<>();
        mAdapter=new SearchAdapter(getActivity(),mData);
//        //判断网络是否可用
//        boolean usable=mConnectivityManager.getActiveNetworkInfo().isAvailable();

        loadData();
    }

    private void loadData() {
        mPath = NetConfig.SEARCH_PATH;
        RequestParams params=new RequestParams(mPath);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                //数据库存储
                myDb = new MyDbBean();

                Calendar calendar=Calendar.getInstance();
                SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
                myDb.time=format.format(calendar.getTime());
                myDb.path=mPath;
                myDb.json=result;
                try {
                    mDbManager.save(myDb);

                } catch (DbException e) {
                    e.printStackTrace();
                }

                try {
                    JSONObject jsonObject=new JSONObject(result);
                    JSONObject object=jsonObject.getJSONObject("data");
                    JSONObject obj=object.getJSONObject("returnData");
                    JSONArray jsonArray=obj.getJSONArray("rankingList");
                    JSONArray jsonArray2=obj.getJSONArray("topList");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObj= (JSONObject) jsonArray.get(i);
                        SearchBean.RankingList list=new SearchBean.RankingList();
                        list.argName=jsonObj.optString("argName");
                        list.argValue=jsonObj.optInt("argValue");
                        list.cover=jsonObj.getString("cover");
                        list.sortName=jsonObj.optString("sortName");
                        mData.add(list);
                    }
                    mAdapter.notifyDataSetChanged();

                    for (int i = 0; i <jsonArray2.length() ; i++) {
                        JSONObject jsonObj2 = (JSONObject) jsonArray2.get(i);
                        SearchBean.TopList topList=new SearchBean.TopList();
                        topList.cover=jsonObj2.optString("cover");
                        topList.sortId=jsonObj2.optString("sortId");
                        topList.sortName=jsonObj2.optString("sortName");
//                        topList.extra=jsonObj2.optJSONArray("extra");
                        JSONObject extraObj=jsonObj2.getJSONObject("extra");
                        SearchBean.Extra extra=new SearchBean.Extra();
                        extra.title=extraObj.optString("title");

                        JSONArray array=extraObj.getJSONArray("tabList");
                        for (int j = 0; j < array.length(); j++) {
                            JSONObject jo=array.getJSONObject(j);
                            SearchBean.TabList tabLiat=new SearchBean.TabList();
                            tabLiat.argCon=jo.optInt("argCon");
                            tabLiat.argName=jo.optString("argName");
                            tabLiat.argValue=jo.optInt("argValue");
                            tabLiat.tabTitle=jo.optString("tabTitle");
                            mHeadDataDetail.add(tabLiat);
                        }

                        mHeadData.add(topList);
                    }
                    Picasso.with(getActivity()).load(mHeadData.get(0).cover).into(mImageView1);
                    Picasso.with(getActivity()).load(mHeadData.get(1).cover).into(mImageView2);
                    Picasso.with(getActivity()).load(mHeadData.get(2).cover).into(mImageView3);
                    Picasso.with(getActivity()).load(mHeadData.get(3).cover).into(mImageView4);

                    mImageView1.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent=new Intent(getActivity(),SearchTopListDetailActivity.class);

                            intent.putExtra("title",mHeadData.get(0).sortName);
                            intent.putExtra("argValue",mHeadDataDetail.get(0).argValue);
                            intent.putExtra("tabTitle",mHeadDataDetail.get(0).tabTitle);
                            intent.putExtra("argCon",mHeadDataDetail.get(0).argCon);
                            intent.putExtra("argName",mHeadDataDetail.get(0).argName);

                            intent.putExtra("argValue2",mHeadDataDetail.get(1).argValue);
                            intent.putExtra("tabTitle2",mHeadDataDetail.get(1).tabTitle);
                            intent.putExtra("argCon2",mHeadDataDetail.get(1).argCon);
                            intent.putExtra("argName2",mHeadDataDetail.get(1).argName);
                            startActivity(intent);
                        }
                    });
                    mImageView2.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent=new Intent(getActivity(),SearchTopListDetailActivity.class);
                            intent.putExtra("title",mHeadData.get(1).sortName);
                            intent.putExtra("argValue",mHeadDataDetail.get(2).argValue);
                            intent.putExtra("tabTitle",mHeadDataDetail.get(2).tabTitle);
                            intent.putExtra("argCon",mHeadDataDetail.get(2).argCon);
                            intent.putExtra("argName",mHeadDataDetail.get(2).argName);

                            intent.putExtra("argValue2",mHeadDataDetail.get(3).argValue);
                            intent.putExtra("tabTitle2",mHeadDataDetail.get(3).tabTitle);
                            intent.putExtra("argCon2",mHeadDataDetail.get(3).argCon);
                            intent.putExtra("argName2",mHeadDataDetail.get(3).argName);
                            startActivity(intent);
                        }
                    });
                    mImageView3.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent=new Intent(getActivity(),SearchTopListDetailActivity.class);
                            intent.putExtra("title",mHeadData.get(2).sortName);
                            intent.putExtra("argValue",mHeadDataDetail.get(4).argValue);
                            intent.putExtra("tabTitle",mHeadDataDetail.get(4).tabTitle);
                            intent.putExtra("argCon",mHeadDataDetail.get(4).argCon);
                            intent.putExtra("argName",mHeadDataDetail.get(4).argName);

                            intent.putExtra("argValue2",mHeadDataDetail.get(5).argValue);
                            intent.putExtra("tabTitle2",mHeadDataDetail.get(5).tabTitle);
                            intent.putExtra("argCon2",mHeadDataDetail.get(5).argCon);
                            intent.putExtra("argName2",mHeadDataDetail.get(5).argName);
                            startActivity(intent);
                        }
                    });
                    mImageView4.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent=new Intent(getActivity(),SearchTopListDetailActivity.class);
                            intent.putExtra("title",mHeadData.get(3).sortName);
                            intent.putExtra("argValue",mHeadDataDetail.get(6).argValue);
                            intent.putExtra("tabTitle",mHeadDataDetail.get(6).tabTitle);
                            intent.putExtra("argCon",mHeadDataDetail.get(6).argCon);
                            intent.putExtra("argName",mHeadDataDetail.get(6).argName);

                            intent.putExtra("argValue2",mHeadDataDetail.get(7).argValue);
                            intent.putExtra("tabTitle2",mHeadDataDetail.get(7).tabTitle);
                            intent.putExtra("argCon2",mHeadDataDetail.get(7).argCon);
                            intent.putExtra("argName2",mHeadDataDetail.get(7).argName);
                            startActivity(intent);
                        }
                    });

                } catch (Exception e) {
                    e.printStackTrace();
                }


            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                try {
//                mData.clear();
                    List<MyDbBean> data=mDbManager.selector(MyDbBean.class).where("path","=",mPath).findAll();
                    JSONObject jsonObject=new JSONObject(data.get(0).json);
                    JSONObject object=jsonObject.getJSONObject("data");
                    JSONObject obj=object.getJSONObject("returnData");
                    JSONArray jsonArray=obj.getJSONArray("rankingList");
                    JSONArray jsonArray2=obj.getJSONArray("topList");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObj= (JSONObject) jsonArray.get(i);
                        SearchBean.RankingList list=new SearchBean.RankingList();
                        list.argName=jsonObj.optString("argName");
                        list.argValue=jsonObj.optInt("argValue");
                        list.cover=jsonObj.getString("cover");
                        list.sortName=jsonObj.optString("sortName");
                        mData.add(list);
                    }
                    mAdapter.notifyDataSetChanged();

                    for (int i = 0; i <jsonArray2.length() ; i++) {
                        JSONObject jsonObj2 = (JSONObject) jsonArray2.get(i);
                        SearchBean.TopList topList=new SearchBean.TopList();
                        topList.cover=jsonObj2.optString("cover");
                        topList.sortId=jsonObj2.optString("sortId");
                        topList.sortName=jsonObj2.optString("sortName");

                        JSONObject extraObj=jsonObj2.getJSONObject("extra");
                        SearchBean.Extra extra=new SearchBean.Extra();
                        extra.title=extraObj.optString("title");

                        JSONArray array=extraObj.getJSONArray("tabList");
                        for (int j = 0; j < array.length(); j++) {
                            JSONObject jo=array.getJSONObject(j);
                            SearchBean.TabList tabLiat=new SearchBean.TabList();
                            tabLiat.argCon=jo.optInt("argCon");
                            tabLiat.argName=jo.optString("argName");
                            tabLiat.argValue=jo.optInt("argValue");
                            tabLiat.tabTitle=jo.optString("tabTitle");
                            mHeadDataDetail.add(tabLiat);
                        }

                        mHeadData.add(topList);
                    }
                    Picasso.with(getActivity()).load(mHeadData.get(0).cover).into(mImageView1);
                    Picasso.with(getActivity()).load(mHeadData.get(1).cover).into(mImageView2);
                    Picasso.with(getActivity()).load(mHeadData.get(2).cover).into(mImageView3);
                    Picasso.with(getActivity()).load(mHeadData.get(3).cover).into(mImageView4);

                    mImageView1.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent=new Intent(getActivity(),SearchTopListDetailActivity.class);

                            intent.putExtra("title",mHeadData.get(0).sortName);
                            intent.putExtra("argValue",mHeadDataDetail.get(0).argValue);
                            intent.putExtra("tabTitle",mHeadDataDetail.get(0).tabTitle);
                            intent.putExtra("argCon",mHeadDataDetail.get(0).argCon);
                            intent.putExtra("argName",mHeadDataDetail.get(0).argName);

                            intent.putExtra("argValue2",mHeadDataDetail.get(1).argValue);
                            intent.putExtra("tabTitle2",mHeadDataDetail.get(1).tabTitle);
                            intent.putExtra("argCon2",mHeadDataDetail.get(1).argCon);
                            intent.putExtra("argName2",mHeadDataDetail.get(1).argName);
                            startActivity(intent);
                        }
                    });
                    mImageView2.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent=new Intent(getActivity(),SearchTopListDetailActivity.class);
                            intent.putExtra("title",mHeadData.get(1).sortName);
                            intent.putExtra("argValue",mHeadDataDetail.get(2).argValue);
                            intent.putExtra("tabTitle",mHeadDataDetail.get(2).tabTitle);
                            intent.putExtra("argCon",mHeadDataDetail.get(2).argCon);
                            intent.putExtra("argName",mHeadDataDetail.get(2).argName);

                            intent.putExtra("argValue2",mHeadDataDetail.get(3).argValue);
                            intent.putExtra("tabTitle2",mHeadDataDetail.get(3).tabTitle);
                            intent.putExtra("argCon2",mHeadDataDetail.get(3).argCon);
                            intent.putExtra("argName2",mHeadDataDetail.get(3).argName);
                            startActivity(intent);
                        }
                    });
                    mImageView3.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent=new Intent(getActivity(),SearchTopListDetailActivity.class);
                            intent.putExtra("title",mHeadData.get(2).sortName);
                            intent.putExtra("argValue",mHeadDataDetail.get(4).argValue);
                            intent.putExtra("tabTitle",mHeadDataDetail.get(4).tabTitle);
                            intent.putExtra("argCon",mHeadDataDetail.get(4).argCon);
                            intent.putExtra("argName",mHeadDataDetail.get(4).argName);

                            intent.putExtra("argValue2",mHeadDataDetail.get(5).argValue);
                            intent.putExtra("tabTitle2",mHeadDataDetail.get(5).tabTitle);
                            intent.putExtra("argCon2",mHeadDataDetail.get(5).argCon);
                            intent.putExtra("argName2",mHeadDataDetail.get(5).argName);
                            startActivity(intent);
                        }
                    });
                    mImageView4.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent=new Intent(getActivity(),SearchTopListDetailActivity.class);
                            intent.putExtra("title",mHeadData.get(3).sortName);
                            intent.putExtra("argValue",mHeadDataDetail.get(6).argValue);
                            intent.putExtra("tabTitle",mHeadDataDetail.get(6).tabTitle);
                            intent.putExtra("argCon",mHeadDataDetail.get(6).argCon);
                            intent.putExtra("argName",mHeadDataDetail.get(6).argName);

                            intent.putExtra("argValue2",mHeadDataDetail.get(7).argValue);
                            intent.putExtra("tabTitle2",mHeadDataDetail.get(7).tabTitle);
                            intent.putExtra("argCon2",mHeadDataDetail.get(7).argCon);
                            intent.putExtra("argName2",mHeadDataDetail.get(7).argName);
                            startActivity(intent);
                        }
                    });

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }


    private void setData() {
        mGridView.setAdapter(mAdapter);

    }

    private void initView(View view) {
        mGridView= (MyGridView) view.findViewById(R.id.gv_search_fragment);
        mImageView1= (ImageView) view.findViewById(R.id.iv_search_item_head_1);
        mImageView2= (ImageView) view.findViewById(R.id.iv_search_item_head_2);
        mImageView3= (ImageView) view.findViewById(R.id.iv_search_item_head_3);
        mImageView4= (ImageView) view.findViewById(R.id.iv_search_item_head_4);
        mButton= (Button) view.findViewById(R.id.btn_search_fragment);
    }

}
